package com.didispace.chapter310.service;

import com.didispace.chapter310.service.obs.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * UserController.
 *
 * @date 2021/6/30 1:09 下午
 * @since 1.0.0
 */

@RestController
public class UserController {

  @Autowired
  private UserService userService;
  @Autowired
  private Test test;

  @GetMapping("/register")
  public String register(){
    return userService.register();
  }

  @GetMapping("/test/{id}")
  public String test(@PathVariable Integer id) throws Exception {
    switch (id) {
      case 1:
        userService.test1();
        break;
      case 2:
        userService.test2();
        break;
      case 3:
        userService.test3();
        break;
      case 4:
        userService.test4();
        break;
      case 5:
        userService.test5();
      case 6:
        userService.test7();
        break;
      case 7:
        userService.test9();
        break;
      case 8:
        userService.test11();
        break;
      case 9:
        userService.test12();
        break;
      case 10:
        userService.test13();
        break;
      case 11:
        userService.test14();
        break;
      case 12:
        userService.test15();
      case 13:
        userService.test16();
      case 14:
        userService.test17();
      case 15:
        userService.test19();
      case 16:
        userService.test21();
      case 17:
        test.test1();
      case 18:
        userService.test165();
      case 19:
        userService.test1655();
      case 20:
        userService.test16555();
      case 21:
        userService.test165555();
      case 22:
        userService.test1655555();
        break;
      default:
        break;
    }
    return "test method userService.test" + id;
  }




    /**
     * @param @return 参数说明
     * @return BaseRestResult 返回类型
     * @Description: 生成滑块拼图验证码
     */
    @RequestMapping(value = "/getImageVerifyCode", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Map getImageVerifyCode(HttpServletRequest request) {
      Map<String, Object> resultMap = new HashMap<>();
      //读取本地路径下的图片,随机选一条
      String path = this.getClass().getClassLoader().getResource("").getPath()+"/image";
      File file = new File(path);
      File[] files = file.listFiles();
      int n = new Random().nextInt(files.length);
      File imageUrl = files[n];
      ImageUtil.createImage(imageUrl, resultMap);

      //读取网络图片
      //ImageUtil.createImage("http://hbimg.b0.upaiyun.com/7986d66f29bfeb6015aaaec33d33fcd1d875ca16316f-2bMHNG_fw658",resultMap);
      request.getSession().setAttribute("xWidth", resultMap.get("xWidth"));
      resultMap.remove("xWidth");
      resultMap.put("errcode", 0);
      resultMap.put("errmsg", "success");
      return resultMap;
    }


  /**
   * 校验滑块拼图验证码
   *
   * @param moveLength 移动距离
   * @return BaseRestResult 返回类型
   * @Description: 生成图形验证码
   */
  @RequestMapping(value = "/verifyImageCode.do", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
  public Map  verifyImageCode(HttpServletRequest request,@RequestParam(value = "moveLength") String moveLength) {

      Double dMoveLength = Double.valueOf(moveLength);
      Map<String, Object> resultMap = new HashMap<>();
      try {
        Integer xWidth = (Integer) request.getSession().getAttribute("xWidth");
        if (xWidth == null) {
          resultMap.put("errcode", 1);
          resultMap.put("errmsg", "验证过期，请重试");
          return resultMap;
        }
        if (Math.abs(xWidth - dMoveLength) > 10) {
          resultMap.put("errcode", 1);
          resultMap.put("errmsg", "验证不通过");
        } else {
          resultMap.put("errcode", 0);
          resultMap.put("errmsg", "验证通过");
        }
      } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
      } finally {
        request.getSession().removeAttribute("xWidth");
      }
      return resultMap;
    }

}
